package com.day03;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 上官
 * @date 2024/12/28
 */
public class Caller {

    List<Listener> list = new ArrayList<>();

    public void initListener() {
        list.add(new DemoOneListener());
        list.add(new DemoTwoListener());
    }

    public void call() {
        this.initListener();
        this.execute("start", listener -> {
            listener.start();
        });
        this.execute("running", listener -> {
            listener.running();
        });
        finish("finish", list.get(0));
        finish("finish", list.get(1));
    }

    /**
     * forEach会调用Consumer执行accept方法，传递参数是Listener
     */
    public void execute(String step, Consumer<Listener> consumer) {
        System.out.println("执行信息：" + step);
        list.forEach(consumer);
    }

    public <T extends Listener> void finish(String step, T listener) {
        System.out.println("执行信息：" + step);
        Listener call = listener;
        call.finish();
    }

    public static void main(String[] args) {
        Caller caller = new Caller();
        caller.call();
    }
}
